<template>
  <div class="header-container">
    <div class="header-items-container">
      <div class="header-logo" @click="gotoHome">随手递</div>
      <div v-if="nowUser.role === 1" v-bind:class="{ 'header-now-item': isShop, 'header-item': !isShop }" @click="gotoShop">我的商店</div>
      <div v-if="nowUser.role === 1" v-bind:class="{ 'header-now-item': isProducts, 'header-item': !isProducts }" @click="gotoProducts">商店管理</div>
      <div v-if="nowUser.role === 0" v-bind:class="{ 'header-now-item': isStores, 'header-item': !isStores }" @click="gotoStores">首页</div>
      <div v-if="nowUser.role === 0" v-bind:class="{ 'header-now-item': isOrders, 'header-item': !isOrders }" @click="gotoOrders">我的订单</div>
      <div class="header-user">
        <el-dropdown v-if="nowUser !== null" @command="handleCommand">
          <span style="color: #ffffff;">
            <i class="far fa-user"></i>
            &nbsp;{{ nowUser.username }}&nbsp;
            <i class="el-icon-arrow-down"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="gotoUserCenter">个人中心</el-dropdown-item>
            <!-- <el-dropdown-item>我的钱包</el-dropdown-item> -->
            <el-dropdown-item command="logout" divided>退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex'
import { ROUTENAME } from 'services/constConfig.js'

export default {
  data() {
    return {
      isShop: this.judgeIsShop(),
      isProducts: this.judgeIsProducts(),
      isStores: this.judgeIsStores(),
      isStore: this.judgeIsStore(),
      isOrders: this.judgeIsOrders()
    }
  },
  computed: mapGetters({
    nowUser: 'nowUser'
  }),
  methods: {
    ...mapActions({
      setNowUser: 'setNowUser'
    }),
    judgeIsShop() {
      return this.$route.path === '/shop'
    },
    judgeIsProducts() {
      return this.$route.path === '/products'
    },
    judgeIsStores() {
      return this.$route.path === '/stores'
    },
    judgeIsStore() {
      return this.$route.path === '/store'
    },
    judgeIsOrders() {
      return this.$route.path === '/user/orders'
    },
    gotoHome() {
      this.$router.push({ path: ROUTENAME.HOME })
    },
    gotoShop() {
      this.$router.push({ path: ROUTENAME.SHOP })
    },
    gotoProducts() {
      this.$router.push({ path: ROUTENAME.PRODUCTS })
    },
    gotoStores() {
      this.$router.push({ path: ROUTENAME.STORES })
    },
    gotoOrders() {
      this.$router.push({ path: ROUTENAME.USER_ORDER })
    },
    logout() {
      this.setNowUser(null)
      this.$router.push({ path: ROUTENAME.HOME })
    },
    handleCommand(command) {
      if (command === 'logout') {
        this.logout()
      }
      if (command === 'gotoUserCenter') {
        if (this.nowUser.role === 1) {
          this.$router.push({ path: ROUTENAME.SELLER })
        }
        if (this.nowUser.role === 0) {
          this.$router.push({ path: ROUTENAME.USER })
        }
      }
    }
  }
}
</script>
<style scoped>
.header-container {
  width: 100%;
  height: 60px;
  background-color: #1e89e0;
}
.header-items-container {
  width: 1200px;
  height: 60px;
  /* background-color: red; */
  margin-left: auto;
  margin-right: auto;
}
.header-logo {
  height: 60px;
  line-height: 60px;
  float: left;
  /* margin-left: 10px; */
  margin-right: 30px;
  font-size: 26px;
  color: #ffffff;
}
.header-item {
  height: 60px;
  line-height: 60px;
  float: left;
  font-size: 16px;
  color: #ffffff;
  padding-left: 30px;
  padding-right: 30px;
}
.header-now-item {
  height: 60px;
  line-height: 60px;
  float: left;
  font-size: 16px;
  color: #ffffff;
  padding-left: 30px;
  padding-right: 30px;
  background-color: #006bc7;
}
.header-item:hover {
  cursor: pointer;
  background-color: #0c77d1;
}
.header-now-item:hover {
  cursor: pointer;
  /* background-color: #0c77d1; */
}
.header-logo:hover {
  cursor: pointer;
}
.header-user {
  float: right;
  height: 60px;
  line-height: 60px;
  font-size: 20px;
  color: #ffffff;
}
.header-user:hover {
  cursor: pointer;
}
</style>